package com.wpam.scrabble.mobile.ui;

import com.wpam.scrabble.mobile.GameResources;
import com.wpam.scrabble.mobile.LetterManager;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class LettersStandSquare extends SquareBitmap {

	private int initialPosX_;
	private int initialPosY_;
	private int pickedUpSizeX_;
	private int pickedUpSizeY_;
	private String currentLetter_;
	private boolean usable_;
	private boolean isPickedUp_;
	private int pickedOffsetX_;
	private int pickedOffsetY_;
	private Bitmap letterSlot_;
	
	public LettersStandSquare(int x, int y, int size) {
		
		super(x, y, size, SquareBitmap.BLANK);
		this.initialPosX_ = x;
		this.initialPosY_ = y;
		this.pickedUpSizeX_ = (int) (size * 1.2);
		this.pickedUpSizeY_ = (int) (size * 1.2);
		this.pickedOffsetX_ = (int) (size / 2.0);
		this.pickedOffsetY_ = (int) (size / 2.0);
		this.currentLetter_ = "";
		this.letterSlot_ = Bitmap.createScaledBitmap(GameResources.getInstance().getField(), size, size, true);
		reset();
	}

	@Override
	public void draw(Canvas canvas) {

		if (isPickedUp_)
			canvas.drawBitmap(letterSlot_, initialPosX_, initialPosY_, bitmapPaint_);
		super.draw(canvas);
	}

	public void onResize(int x, int y, int size) {

		this.initialPosX_ = x;
		this.initialPosY_ = y;
		this.pickedUpSizeX_ = (int) (size * 1.2);
		this.pickedUpSizeY_ = (int) (size * 1.2);
		this.pickedOffsetX_ = (int) (size / 2.0);
		this.pickedOffsetY_ = (int) (size / 2.0);
		this.posX_ = initialPosX_;
		this.posY_ = initialPosY_;
		this.size_ = size;
		this.background_ = Bitmap.createScaledBitmap(background_, size_, size_, true);
		this.letterSlot_ = Bitmap.createScaledBitmap(GameResources.getInstance().getField(), size, size, true);
		this.letterOffsetX_ = (int) (size_ * 0.45);
		this.letterOffsetY_ = (int) (size_ * 0.85);
		this.pointOffsetX_ = (int) (size_ * 0.85);
		this.pointOffsetY_ = (int) (size_ * 0.9);
		adjustTextSize();
	}

	@Override
	public void reset() {

		this.posX_ = initialPosX_;
		this.posY_ = initialPosY_;
		this.usable_ = true;
		this.isPickedUp_ = false;
		adjustTextSize();
		super.reset();
		this.letter_ = currentLetter_;
		if (currentLetter_ != null)
			this.points_ = LetterManager.getPoints(currentLetter_);
	}
	
	@Override
	public boolean setLetter(String letter) {
		
		this.currentLetter_ = letter;
		return super.setLetter(letter);
	}
	
	public boolean amIHit(int x, int y) {
		
		if (x >= posX_ && x <= posX_ + size_ 
				&& y >= posY_ && y <= posY_ + size_)
			return true;
		return false;
	}
	
	public LettersStandSquare pickUp() {
		
		if (this.usable_) {
			this.isPickedUp_ = true;
			this.background_ = Bitmap.createScaledBitmap(background_, pickedUpSizeX_, pickedUpSizeY_, true);
			return this;
		}				
		return null;
	}
	
	public void putDown(boolean status) {

		this.isPickedUp_ = false;
		this.posX_ = initialPosX_;
		this.posY_ = initialPosY_;
		if (status) {
			this.background_ = GameResources.getInstance().getField();
			this.usable_ = false;
			this.letter_ = "";
		}
		this.background_ = Bitmap.createScaledBitmap(background_, size_, size_, true);
	}

	public void setUsable(boolean usable) {
		
		this.usable_ = usable;
	}

	public int getPickedOffsetX() {
		return pickedOffsetX_;
	}

	public int getPickedOffsetY() {
		return pickedOffsetY_;
	}

	public boolean isPickedUp() {
		return isPickedUp_;
	}
}
